package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.AppCxt;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.crm.entity.GwaaT;
import cn.e4j.modular.crm.mapper.GwaaTMapper;
import cn.e4j.modular.system.entity.SysRole;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.entity.SysUserDevice;
import cn.e4j.modular.system.entity.SysUserRole;
import cn.e4j.modular.system.mapper.SysRoleMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 注册信息 服务实现类
 *
 * @author yuan_rz
 * @since 2019-01-15
 */
@Service
public class Gwai120ServiceImpl extends ServiceImpl<GwaaTMapper, GwaaT> {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Transactional(readOnly = true)
    public List<Dto> getListData(Page<Object> page, Dto dto) {
//        List<Dto> listData = this.baseMapper.getListData(page, dto);
        List<Dto> listData = InvokeUtil.invokeBeanMethod("gwaaTMapper,getListData",page,dto);
        for (int i = 0; i < listData.size(); i++) {
            List<Dto> sysUserRole = this.baseMapper.getSysUserRole(listData.get(i).getString("GWAA003"));
            if (sysUserRole != null) {
                if (sysUserRole.size() > 0) {
                    String role = sysUserRole.get(0).getString("ROLE_ID");
                    listData.get(i).put("ROLE", role);
                    SysRole role1 = sysRoleMapper.selectById(role);
                    if (role1 != null) {
                        listData.get(i).put("ROLE_NAME", role1.getNAME());
                    }
                }
            }
        }
        return listData;
    }

    @Transactional(readOnly = true)
    public Boolean existedTheParams(GwaaT gwaaT) {
        QueryWrapper<GwaaT> gwaaTQueryWrapper = new QueryWrapper<>();
        gwaaTQueryWrapper.eq("GWAAENT", gwaaT.getGWAAENT());
        gwaaTQueryWrapper.eq("GWAA003", gwaaT.getGWAA003());
        return this.baseMapper.selectList(gwaaTQueryWrapper).size() > 0;
    }

    @Transactional
    public void addData(GwaaT gwaaT, Dto dto) {
        ShiroUser shiroUser = ShiroKit.getUser();
//        String ID = AppCxt.timeId();
        String ID = gwaaT.getGWAA003();
        gwaaT.setGWAA001(ID);
        gwaaT.insert();//gwaa新增

        SysUser sysUser = this.baseMapper.getSysUser(gwaaT.getGWAA003());
        if (sysUser != null) {
            editSysUser(sysUser, gwaaT);
        } else {
            sysUser = new SysUser();
            sysUser.setID(AppCxt.timeId());
            sysUser.setACCOUNT(gwaaT.getGWAA003());//账号
            sysUser.setCREATE_BY(shiroUser.getID());//创建人
            sysUser.setCREATE_TIME(LocalDateTime.now());//创建时间
            sysUser.setIS_DEL("0");//是否已删除
            sysUser.setPASSWORD(ShiroKit.md5("******"));
            sysUser.setNAME(gwaaT.getGWAA002());//姓名
            sysUser.setSTATUS("1");//状态
            sysUser.setTYPE(gwaaT.getGWAA007());//用户类型
            sysUser.setORG_ID(gwaaT.getGWAA008());//所属组织
            sysUser.setEMAIL(gwaaT.getGWAA011());//电子邮件
            sysUser.setMOBILE(gwaaT.getGWAA014());//联系电话
            sysUser.setSKIN("blue");
            sysUser.setLANG("zh_CN");
            sysUser.setDBC(shiroUser.getDBC());
            sysUser.setADDRESS(gwaaT.getGWAA013());
            sysUser.setSEX(gwaaT.getGWAA006());//0-未知 1-男 2-女
            sysUser.insert();//SysUser新增
            if (!dto.getString("ROLE").equals("")) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setID(AppCxt.timeId());
                sysUserRole.setROLE_ID(dto.getString("ROLE"));
                sysUserRole.setUSER_ID(sysUser.getID());
                sysUserRole.insert();
            }
        }
    }

    @Transactional
    public void editData(GwaaT gwaaT, Dto dto) {
        UpdateWrapper<GwaaT> gwaaTUpdateWrapper = new UpdateWrapper<>();
        gwaaTUpdateWrapper.eq("GWAAENT", gwaaT.getGWAAENT());
        gwaaTUpdateWrapper.eq("GWAA003", gwaaT.getGWAA003());
        gwaaT.update(gwaaTUpdateWrapper);//gwaa更新

        SysUser sysUser = this.baseMapper.getSysUser(gwaaT.getGWAA003());
        if (sysUser != null) {
            editSysUser(sysUser, gwaaT);
            if(gwaaT.getGWAAACTI().equals("Y") && gwaaT.getGWAA007().equals("1")) {
                Dto inDto = Dtos.newDto();
                inDto.put("ID", sysUser.getID());
                List<Dto> gwae = this.baseMapper.getSysUserDevice(inDto);
                SysUserDevice sysUserDevice = new SysUserDevice();
                if(gwae.size() == 0) {
                    sysUserDevice.setID(AppCxt.timeId());
                    sysUserDevice.setUSER_ID(sysUser.getID());
                    sysUserDevice.setDEVICE_ID(gwaaT.getGWAA022());
                    sysUserDevice.insert();
                }else{
                    sysUserDevice.setDEVICE_ID(gwaaT.getGWAA022());
                    UpdateWrapper<SysUserDevice> userDeviceUpdateWrapper = new UpdateWrapper<>();
                    userDeviceUpdateWrapper.eq("USER_ID", sysUser.getID());
                    sysUserDevice.update(userDeviceUpdateWrapper);
                }
            }
        }
        List<Dto> sysUserRole = this.baseMapper.getSysUserRole(gwaaT.getGWAA003());
        if (sysUserRole != null) {
            if (sysUserRole.size() > 0) {
                SysUserRole role = new SysUserRole();
                role.setID(sysUserRole.get(0).getString("ID"));
                role.setROLE_ID(dto.getString("ROLE"));
                role.updateById();
            } else {
                SysUserRole role = new SysUserRole();
                role.setID(AppCxt.timeId());
                role.setROLE_ID(dto.getString("ROLE"));
                role.setUSER_ID(sysUser.getID());
                role.insert();
            }
        }
    }

    @Transactional
    public void deleteData(GwaaT gwaaT) {
        delete(gwaaT);
    }

    @Transactional
    public void deleteDataBatch(List<GwaaT> gwaaTList) {
        for (GwaaT gwaaT : gwaaTList) {
            delete(gwaaT);
        }
    }

    @Transactional(readOnly = true)
    public List<Dto> getProvinceCityList(Dto inDto) {
        List<Dto> provinceList = this.baseMapper.getProvinceList(inDto);//省数据
        for (Map province : provinceList) {
            inDto.put("gwad003", province.get("CODE"));
            province.put("cityList", this.baseMapper.getCityList(inDto));//市数据
            Dto dto = Dtos.newDto();
            dto.put("ENT", inDto.getString("ENT"));
            dto.put("LANG", inDto.getString("LANG"));
            dto.put("GWAD001", "3");
            dto.put("GWAD002", province.get("GWAD003"));
            Map<String, Object> result = this.baseMapper.getGwadData(dto);
            province.put("country", result.get("COUNTRY"));//国家
            province.put("continent", result.get("CONTINENT"));//洲
        }
        return provinceList;
    }

    @Transactional(readOnly = true)
    public List<Dto> getKhhyList(Dto inDto) {
        List<Dto> listData = this.baseMapper.getKhhyList(inDto);
        return listData;
    }

    @Transactional(readOnly = true)
    public List<Dto> getGsdxList(Page<Object> page,Dto inDto) {
        List<Dto> listData = this.baseMapper.getGsdxList(page, inDto);
        return listData;
    }

    private void delete(GwaaT gwaaT) {
        UpdateWrapper<GwaaT> gwaaTUpdateWrapper = new UpdateWrapper<>();
        gwaaTUpdateWrapper.eq("GWAAENT", gwaaT.getGWAAENT());
        gwaaTUpdateWrapper.eq("GWAA003", gwaaT.getGWAA003());
        gwaaT.delete(gwaaTUpdateWrapper);//gwaa删除

        SysUser sysUser = this.baseMapper.getSysUser(gwaaT.getGWAA003());
        List<Dto> sysUserRole = this.baseMapper.getSysUserRole(gwaaT.getGWAA003());
        if (sysUser != null) {
            sysUser.deleteById();//sysUser删除
        }
        if (sysUserRole != null) {
            if (sysUserRole.size() > 0) {
                SysUserRole role = new SysUserRole();
                role.setID(sysUserRole.get(0).getString("ID"));
                role.deleteById();
            }
        }
    }

    private void editSysUser(SysUser sysUser, GwaaT gwaaT) {
        sysUser.setNAME(gwaaT.getGWAA002());//姓名
        sysUser.setORG_ID(gwaaT.getGWAA008());//所属组织
        sysUser.setEMAIL(gwaaT.getGWAA011());//电子邮件
        sysUser.setMOBILE(gwaaT.getGWAA014());//联系电话
        sysUser.setADDRESS(gwaaT.getGWAA013());
        sysUser.setSEX(gwaaT.getGWAA006());//0-未知 1-男 2-女
        sysUser.setTYPE(gwaaT.getGWAA007());
        if ("X".equals(gwaaT.getGWAAACTI())) {
            sysUser.setSTATUS("3");//1-正常 2-锁定 3-停用
        } else {
            sysUser.setSTATUS("1");
        }
        sysUser.updateById();//SysUser修改
    }

    @Transactional(readOnly = true)
    public List<Dto> getRolesList(Page<Object> page, Dto dto) {
        List<Dto> listData = this.baseMapper.listRolesPage(page, dto);
        return listData;
    }
}
